package chat;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EmptyBorder;

public class ChattingTest1 extends JFrame {

	ChattingPanel1 chattingPanel1 = null;
	int y = 10;
		
	private JPanel contentPane;
	private JPanel panel;
	private JLabel lblNewLabel;
	private JPanel panel_1;
	private JTextField textField;
	private JButton btn_transmit;
	private JScrollPane scrollPane;
	private JPanel panel_Conversation;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ChattingTest1 frame = new ChattingTest1();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public void sendMsg(){
		String message = textField.getText();
		Font f = new Font("맑은 고딕", Font.PLAIN, 12);
	    Rectangle2D r = f.getStringBounds(message, new FontRenderContext
	    		(null, RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT, RenderingHints.VALUE_FRACTIONALMETRICS_DEFAULT));
		
	    double width = r.getWidth();
		double height = r.getHeight();
		
		chattingPanel1 = new ChattingPanel1(message);
		chattingPanel1.setPanel(this);
		chattingPanel1.setVisible(true);
		
		int b = panel_Conversation.getWidth();
		/*if(width>=b/2){
			double a = 1+width/(b/2);
			height *= a;
			chattingPanel1.setBounds(5, y, b/2, (int) (height*2));
		}else if(width-30>(b/2)){
			chattingPanel1.setBounds(5, y, width, height*2);
		}else{
			chattingPanel1.setBounds(5, y, b/2,  (int) (height*2));
		}*/
		
		//	내가 보낼때
		 if(width>=b/2){
			 double a = 1+width/(b/2);
			height *= a;
			chattingPanel1.setBounds(b/2-5, y, b/2, (int) height*2);
		}else{
			chattingPanel1.setBounds((int)(panel_Conversation.getWidth()-width-10)-2, y, (int)(width)+1, (int) height*2);
		}
		y += height*2+10; 
		panel_Conversation.add(chattingPanel1);
		panel_Conversation.updateUI();
		panel_Conversation.setPreferredSize(new Dimension(300,y));
		scrollPane.updateUI();
		double ad = panel_Conversation.getMaximumSize().getHeight();
		scrollPane.getVerticalScrollBar().setValue(1+(int) ad);
		
		textField.setText("");
	}
	/**
	 * Create the frame.
	 */
	public ChattingTest1() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 320, 500);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		contentPane.add(getPanel(), BorderLayout.CENTER);
		contentPane.add(getLblNewLabel(), BorderLayout.NORTH);
		contentPane.add(getPanel_1(), BorderLayout.SOUTH);
	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setBorder(null);
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getScrollPane_2(), BorderLayout.CENTER);
		}
		return panel;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("New label");
		}
		return lblNewLabel;
	}
	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.setLayout(new BorderLayout(0, 0));
			panel_1.add(getTextField(), BorderLayout.CENTER);
			panel_1.add(getBtn_transmit(), BorderLayout.EAST);
		}
		return panel_1;
	}
	private JTextField getTextField() {
		if (textField == null) {
			textField = new JTextField();
			textField.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent e) {
					if(e.getKeyCode()==KeyEvent.VK_ENTER){
						sendMsg();
					}
				}
			});
			textField.setColumns(10);
		}
		return textField;
	}
	private JButton getBtn_transmit() {
		if (btn_transmit == null) {
			btn_transmit = new JButton("New button");
			btn_transmit.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					sendMsg();
					
				}				
			});
		}
		return btn_transmit;
	}
	private JScrollPane getScrollPane_2() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportBorder(null);
			scrollPane.setBorder(null);
			scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
			scrollPane.setAutoscrolls(true);
			scrollPane.getPreferredSize();
			scrollPane.setViewportView(getPanel_Conversation());
			scrollPane.setPreferredSize(new Dimension(300,500));
			
		}
		return scrollPane;
	}
	private JPanel getPanel_Conversation() {
		if (panel_Conversation == null) {
			panel_Conversation = new JPanel();
			panel_Conversation.setBorder(null);
			panel_Conversation.setLayout(null);
		}
		return panel_Conversation;
	}
	
}
